home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Justify Layout ƒ / Justify Layout.c next >
Encoding:
C/C++ Source or Header  |  1996-06-18  |  15.6 KB  |  475 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Justify Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
  11. |**|    "LayoutLibrary.c", "ShapeLibrary.c", and "TransformLibrary.c".
  12. |**|
  13. |**|    6/96 bob    Updated #includes to support changed GX Library names.
  14. |**|                Updated the copyright date.
  15. |**|
  16. |**|    ©1990 - 1996  Apple Computer, Inc.
  17. |**|    All rights reserved.
  18. |**|
  19. |**| =====================================================================
  20. \**/
  21.  
  22.  
  23. #include "QDGX shell.h"
  24. #include <GXLayout.h>
  25. #include "LayoutLibrary.h"
  26.  
  27.  
  28. /**\
  29. |**| ---------------------------------------------------------------------
  30. |**| PROTOTYPES
  31. |**| ---------------------------------------------------------------------
  32. \**/
  33.  
  34. // funtions required by shell
  35.  
  36. void    DoSetup            (void);
  37. void    DoDraw            (WindowPtr wind, Boolean updating);
  38. OSErr    DoCreateNew        (void);
  39. void    DoDispose        (WindowPtr wind);
  40. void    DoIdle            (WindowPtr wind);
  41. void    DoTeardown        (void);
  42. void    DoClick            (WindowPtr wind, Point p);
  43.  
  44. // private functions
  45.  
  46. OSErr    DoWindowInit        (WindowPtr wind);
  47. void    CreateSampleImage    (WindowPtr wind);
  48.  
  49.  
  50. /**\
  51. |**| ---------------------------------------------------------------------
  52. |**| ENUMS
  53. |**| ---------------------------------------------------------------------
  54. \**/
  55. enum { rWindResource = 128 };
  56.  
  57.  
  58. /**\
  59. |**| ---------------------------------------------------------------------
  60. |**| GLOBALS
  61. |**| ---------------------------------------------------------------------
  62. \**/
  63. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  64. // functionality will only work with the "debugging" version of QuickDraw GX.
  65. // If the debugging version is not installed, nothing bad will happen, but these
  66. // functions will not work. 
  67.  
  68. Boolean        gDebugging = true;
  69.  
  70. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  71.  
  72. Boolean        gGiveMeValidation = true;
  73.  
  74.  
  75. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  76. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  77. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  78. // since we need abunch if we have several windows open.
  79.  
  80. long        gGraphicsHeapSize = 600;
  81.  
  82. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  83. // override.  This is so that our override can be native PowerPC code if necessary.
  84.  
  85. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  86.  
  87.  
  88.  
  89. /**\
  90. |**| ---------------------------------------------------------------------
  91. |**| DoSetup()
  92. |**| Here's where we initialize any global variables our application needs.
  93. |**| We have only one at this time -- the universal proc pointer for
  94. |**| our printing override.
  95. |**| ---------------------------------------------------------------------
  96. \**/
  97. void DoSetup (void)
  98. {    // Initialize our printing event override UPP
  99.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  100. }
  101.  
  102.  
  103. /**\
  104. |**| ---------------------------------------------------------------------
  105. |**| DoDraw()
  106. |**| Draw the contents of the window.  The first parameter is the window
  107. |**| to draw, and the second parameter is true if we're updating an existing
  108. |**| image.  If that's the case, we don't want to change anything, but
  109. |**| just draw what's already there.
  110. |**| ---------------------------------------------------------------------
  111. \**/
  112. void DoDraw (WindowPtr wind, Boolean updating)
  113. {
  114.      #pragma unused (updating)
  115.      GXDrawShape (GetDocShape(wind));
  116. }
  117.  
  118.  
  119. /**\
  120. |**| ---------------------------------------------------------------------
  121. |**| DoCreateNew()
  122. |**| This routine is called when a window needs to be created.
  123. |**| ---------------------------------------------------------------------
  124. \**/
  125. OSErr DoCreateNew (void)
  126. {
  127.     OSErr        err = noErr;
  128.     WindowPtr    wind;
  129.     
  130. // Get and create our window from the resource fork
  131.  
  132.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  133.  
  134. // Attach a default gxViewPort to it, create and iInitialize our
  135. // private data for it, and add a sample image to its page shape.
  136.  
  137.     if ( wind == NULL )
  138.         return (MemError());
  139.  
  140.     GXIgnoreGraphicsNotice(transform_already_set);
  141.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  142.     GXPopGraphicsNotice();
  143.     
  144.     err = DoWindowInit(wind);
  145.     if ( err != noErr )
  146.         return err;
  147.     
  148.     CreateSampleImage(wind);
  149.     return err;
  150. }
  151.  
  152.  
  153. /**\
  154. |**| ---------------------------------------------------------------------
  155. |**| DoDispose()
  156. |**| This routine is called when a window needs to be disposed of.
  157. |**| ---------------------------------------------------------------------
  158. \**/
  159. void DoDispose (WindowPtr wind)
  160. {
  161.     TH_Doc    doc;
  162.     
  163. // You should always dispose of your GX graphics objects before tossing your window.
  164. // Why?  It's generally good form and this approach guarantees that everything is
  165. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  166. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  167. // with notices set, you will receive a notice that you had not disposed of everything.
  168. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  169.     
  170.     if ( wind != NULL )
  171.     {
  172.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  173.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  174.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  175.         DisposHandle((Handle) doc);            // Dispose of our private data.
  176.         DisposeWindow(wind);                // Dispose of the window.
  177.     }
  178. }
  179.  
  180.  
  181. /**\
  182. |**| ---------------------------------------------------------------------
  183. |**| DoIdle()
  184. |**| This routine is called to do things while idling through the event loop.
  185. |**| ---------------------------------------------------------------------
  186. \**/
  187. void DoIdle (WindowPtr wind)
  188. {
  189. }
  190.  
  191.  
  192. /**\
  193. |**| ---------------------------------------------------------------------
  194. |**| DoTeardown()
  195. |**| This routine is called just before we quit to remove anything 
  196. |**| persistent that might have been setup by DoSetup().
  197. |**| ---------------------------------------------------------------------
  198. \**/
  199. void DoTeardown (void)
  200. {
  201.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  202. }
  203.  
  204.  
  205. /**\
  206. |**| ---------------------------------------------------------------------
  207. |**| DoClick()
  208. |**| ---------------------------------------------------------------------
  209. \**/
  210. void DoClick(WindowPtr window, Point p)
  211. {
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218. /**\
  219. |**| ---------------------------------------------------------------------
  220. |**| DoWindowInit()
  221. |**| In this function we create and initialize the the private document
  222. |**| structure for a new window.  This structure contains the print job and
  223. |**| the shape which is drawn in the window.  We store this data in a handle
  224. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  225. |**| this, rather than using globals, we can create many windows containing
  226. |**| unique print jobs and shapes.
  227. |**| ---------------------------------------------------------------------
  228. \**/
  229. OSErr DoWindowInit (WindowPtr wind)
  230. {
  231.     OSErr    err = noErr;
  232.     gxJob    docJob;
  233.     gxShape    docPage;
  234.     TH_Doc    windDoc;
  235.  
  236.  
  237. // Create the page shape. We set the unique items attribute to make sure that each item
  238. // added to the picture has a unique reference. If this attribute was not set, we would
  239. // not see all copies of anything we add to the shape multiple times -- we'd just see
  240. // the last version added.        
  241.  
  242.     docPage = GXNewShape(gxPictureType);
  243.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  244.     
  245.     
  246. // Create a print job for this document.  This will be the same as the system default until
  247. // the user goes through the dialogs for Page Setup or Print…
  248.  
  249.     err = GXNewJob(&docJob);
  250.     
  251.     
  252. // If there are no errors, create a handle the size of our document structure and store
  253. // the print job and page shape in it.  Store the handle in the window's refCon field so
  254. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  255. // can be used to do this easily.
  256.  
  257.     if ( err == noErr )
  258.     {
  259.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  260.  
  261.         if ( windDoc == NULL )
  262.             err = MemError();
  263.         else
  264.         {
  265.             (*windDoc)->docJob = docJob;
  266.             (*windDoc)->docPage = docPage;
  267.             SetWRefCon(wind, (long) windDoc);
  268.         }
  269.  
  270. // Now install our application override for PrintingEvent so that we can
  271. // support the new movable-modal printing dialog boxes.
  272.  
  273.         GXInstallApplicationOverride(docJob, gxPrintingEventMsg, gOurPrintingOverrideUPP);
  274.  
  275.     }
  276.  
  277.     return err;
  278. }
  279.  
  280.  
  281. /**\
  282. |**| ---------------------------------------------------------------------
  283. |**| CreateSampleImage()
  284. |**| This function creates primitive shapes and adds them to the window's page shape.
  285. |**| ---------------------------------------------------------------------
  286. \**/
  287. void CreateSampleImage (WindowPtr wind)
  288. {
  289.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  290.                                             // in QuickDraw coordinates
  291.     gxShape layout1;                    // the first layout shape we build
  292.     gxShape layout2;                    // the second layout shape we build
  293.     gxShape layout3;                    // the third layout shape we build
  294.     gxShape layout4;                    // the fourth layout shape we build
  295.     gxShape thePage;                    // this window's document shape
  296.     gxPoint    posn;                        // position for our shapes
  297.     gxRunControls     runControls;        // run controls for the layout shape
  298.     gxLayoutOptions layoutOptions;        // options for the layout shape
  299.     StyleRunOverrides overrides;        // style run overrides for our shapes
  300.  
  301. // We use these two structures to assign all justification priority either
  302. // all to the space characters (allToSpace) or all to intercharacter spacing
  303. // (allToChar).
  304.  
  305.     gxPriorityJustificationOverride allToSpace, allToChar;
  306.                                         // 
  307.  
  308.     char *    text1 = "QuickDraw GX Line Layout is a set of support";
  309.     char *    text2 = "routines that applications call to implement";
  310.     char *    text3 = "certain functions. It is not a text editor or";
  311.     char *    text4 = "word processor, but it could help make one.";
  312.  
  313.     
  314. // make default gxLayoutOptions and gxRunControls structures.  These routines are
  315. // found in layout library.c.
  316.  
  317.     InitializeLayoutOptions (&layoutOptions);
  318.     InitializeRunControls (&runControls);
  319.     InitializeStyleRunOverrides(&overrides);
  320.     
  321. // Initialize default gxPriorityJustOverrides structures with another handy
  322. // routine found in layout library.c.
  323.  
  324.     SetDefaultPriorityJustOverride(&allToSpace);
  325.     SetDefaultPriorityJustOverride(&allToChar);
  326.     
  327. // For the allToSpace structure, we set the unlimited override flag as well as
  328. // unlimited gap absorption, which forces all extra width introduced during
  329. // justification to go to the white-space characters.
  330.  
  331.     allToSpace.deltas[gxWhiteSpacePriority].growFlags |= 
  332.         (gxOverrideUnlimited | gxUnlimitedGapAbsorption);
  333.  
  334. // For the allToChar structure, we set the unlimited override flag as well as
  335. // overriding the inter-character priority to "kashida," which is the highest
  336. // priority.  This guarantees that inter-character spacing is adjusted when
  337. // justifying a line.
  338.  
  339.     allToChar.deltas[gxInterCharPriority].growFlags |=
  340.         (gxOverrideUnlimited | gxUnlimitedGapAbsorption | 
  341.          gxOverridePriority | gxKashidaPriority);
  342.     
  343. // We set the text to about twice the width really needed
  344. // so that the justification effects are clearly visible.
  345.  
  346.     layoutOptions.width = ff(6.5 * 72);    
  347.     layoutOptions.just = fract1;            // full justification
  348.     
  349. // To position the lines, we compute the position that would center a 
  350. // layout's baseline in the window. We'll draw the lines 30 points apart,
  351. // so we'll put the first two 45 and 15 points above this position and the
  352. //    second two 15 and 45 points below it.
  353.  
  354.     posn.x = (ff(ourWindowRect.right - ourWindowRect.left) 
  355.                 - layoutOptions.width) / 2;
  356.     posn.y = ff((( ourWindowRect.bottom - ourWindowRect.top) / 2) - 45);
  357.  
  358.  
  359. // Setup complete!  Build the layouts, using NewSingleLayout from layout library.c
  360. // to create layouts with single style runs.  The first and second layouts have no
  361. // options, but the third one has all justification in spaces and the fourth has
  362. // all justification in inter-character spacing.
  363.  
  364.     GXIgnoreGraphicsNotice(text_size_already_set);
  365.     layout1 = NewSingleLayout(
  366.                 text1,                             // the text for the layout
  367.                 (char *) "\pHoefler Text",         // the font name to use
  368.                 ff(12),                         // the point size to use
  369.                 &layoutOptions,                 // our default layout options
  370.                 &posn,                             // the shape's position
  371.                 0,                                 // no text attributes
  372.                 &runControls,                    // our default run controls
  373.                 nil,                             // the run features array (none)
  374.                 0,                                 // count of zero features = 0
  375.                 nil);                            // no style run overrides
  376.     GXPopGraphicsNotice();
  377.     posn.y += ff(30);                            // move down 30 pixels
  378.     
  379.     GXIgnoreGraphicsNotice(text_size_already_set);
  380.     layout2 = NewSingleLayout(
  381.                 text2,                             // the text for the layout
  382.                 (char *) "\pHoefler Text",         // the font name to use
  383.                 ff(12),                         // the point size to use
  384.                 &layoutOptions,                 // our default layout options
  385.                 &posn,                             // the shape's position
  386.                 0,                                 // no text attributes
  387.                 &runControls,                    // our default run controls
  388.                 nil,                             // the run features array (none)
  389.                 0,                                 // count of zero features = 0
  390.                 nil);                            // no style run overrides
  391.     GXPopGraphicsNotice();
  392.  
  393.     posn.y += ff(30);                            // move down 30 pixels
  394.     overrides.priorityJustOverride = &allToSpace;
  395.         
  396.     GXIgnoreGraphicsNotice(text_size_already_set);
  397.     layout3 = NewSingleLayout(
  398.                 text3,                             // the text for the layout
  399.                 (char *) "\pHoefler Text",         // the font name to use
  400.                 ff(12),                         // the point size to use
  401.                 &layoutOptions,                 // our default layout options
  402.                 &posn,                             // the shape's position
  403.                 0,                                 // no text attributes
  404.                 &runControls,                    // our default run controls
  405.                 nil,                             // the run features array (none)
  406.                 0,                                 // count of zero features = 0
  407.                 &overrides);                    // our style run overrides
  408.     GXPopGraphicsNotice();
  409.  
  410.     posn.y += ff(30);                            // move down 30 pixels
  411.     overrides.priorityJustOverride = &allToChar;
  412.         
  413.     GXIgnoreGraphicsNotice(text_size_already_set);
  414.     layout4 = NewSingleLayout(
  415.                 text4,                             // the text for the layout
  416.                 (char *) "\pHoefler Text",         // the font name to use
  417.                 ff(12),                         // the point size to use
  418.                 &layoutOptions,                 // our default layout options
  419.                 &posn,                             // the shape's position
  420.                 0,                                 // no text attributes
  421.                 &runControls,                    // our default run controls
  422.                 nil,                             // the run features array (none)
  423.                 0,                                 // count of zero features = 0
  424.                 &overrides);                    // our style run overrides
  425.     GXPopGraphicsNotice();
  426.  
  427.     
  428. // Retrieve the page shape so we can add to it.
  429.  
  430.     thePage = GetDocShape(wind);
  431.     
  432. // Add the layouts to the window's picture shape.  AddToShape is in shape library.c.
  433.  
  434.     AddToShape(thePage, layout1);
  435.     AddToShape(thePage, layout2);
  436.     AddToShape(thePage, layout3);
  437.     AddToShape(thePage, layout4);
  438.  
  439. // Create two lines to show the margins of these lines, and add them to the picture
  440. // shape.
  441.  
  442.     {
  443.         gxLine    bound;                        // lines showing edges of layouts
  444.         gxShape    lineShape;                    // shape to hold our lines
  445.         
  446.         bound.first.x = posn.x;
  447.         bound.first.y = posn.y - ff(135);
  448.         bound.last.x = posn.x; 
  449.         bound.last.y = posn.y + ff(15);
  450.         lineShape = GXNewLine(&bound);
  451.         AddToShape(thePage, lineShape);
  452.         GXDisposeShape(lineShape);
  453.         
  454.         bound.first.x += layoutOptions.width;
  455.         bound.last.x += layoutOptions. width;
  456.         lineShape = GXNewLine(&bound);
  457.         AddToShape(thePage, lineShape);
  458.         GXDisposeShape(lineShape);
  459.     }
  460.  
  461. // Now that all shapes are added to the picture, dispose of them
  462.  
  463.     GXDisposeShape(layout1);
  464.     GXDisposeShape(layout2);
  465.     GXDisposeShape(layout3);
  466.     GXDisposeShape(layout4);
  467.     
  468.  
  469. // Invalidate the window's portRect so that everything gets updated.
  470.     
  471.     SetPort(wind);
  472.     InvalRect(&ourWindowRect);
  473. }
  474.  
  475.